import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'
//路由规则
const router = createRouter({
  history:createWebHistory(import.meta.env.BASE_URL),
  routes:[
    //帖子展示页
    {
      //根路径
      path:'/',
      component:() => import('@/views/Layout/index.vue'),  
      redirect:'/mainBoard',
      children:[
        //帖子展示页
        {
          path:'mainBoard',
          redirect:'/mainBoard/default',
          component:() => import('@/views/MainBoard/index.vue'),
          children:[
           {
            path:'default',
            component:()=> import('@/views/Article/index.vue')
           },
           {
            path:'new',
            component:()=> import('@/views/Article/newArticle.vue')
           },
           {
            path:'hot',
            component:()=> import('@/views/Article/hotArticle.vue')
           },
           {
            path:'feedback',
            component:()=> import('@/views/Article/feedback/feedback.vue')
           }
          ]
        },
        //消息展示页
        {
          path:'messageBoard',
          redirect:'/messageBoard/feedback',
          component:()=> import('@/views/MessageBoard/index.vue'),
          children:[
            {
              path:'feedback',
              component: () => import('@/views/MessageBoard/feedback.vue')
            },
            {
              path:'comment',
              component: () => import('@/views/MessageBoard/comment.vue')
            },
            {
              path:'unPassArticle',
              component: () => import('@/views/MessageBoard/unPassArticle.vue')
            }
          ]
        },
        //用户主页展示
        {
          path:'userBoard',
          component:()=>import('@/views/UserBoard/index.vue')
        },
        //管理员接口
        {
          path:'admin',
          redirect:'/admin/article',
          component:()=>import('@/views/admin/index.vue'),
          children:[
            //帖子
            {
              path:'article',
              component: () => import('@/views/admin/article/index.vue'),
            },
            //反馈
            {
              path:'feedback',
              component: () => import('@/views/admin/feedback/index.vue'),
            },
              //用户主页
            {
              path:'user',
              component: () => import('@/views/admin/user/index.vue'),
            },
             
             //角色
            {
              path:'role',
              component: () => import('@/views/admin/role/index.vue'),
            },

          ]
        }
      ]
    },
    //帖子详情页
    {
      path:'/detailBox',
      redirect:'/detailBox/article',
      component:()=> import('@/views/Article/detailBox.vue'),
      children:[
        {
          path:'article',
          component: () => import('@/views/Article/detailBox/article.vue')
        }
      ]
    },
    //登录页
    {
      path:'/login',
      component:()=> import ('@/views/Login/index.vue')
    },
    //注册页
    {
      path:'/register',
      component:()=> import('@/views/Register/index.vue')
    },
    //新增帖子
    {
      path:'/addArticle',
      component:()=>import('@/views/Article/addArticle.vue')
    },
    {
      path:'/addFeedback',
      component:()=>import('@/views/Article/feedback/addFeedback.vue')
    }
  ]
})
//白名单
const witeList = ['/login','/register']
//全局路由守卫
// router.beforeEach((to,from)=>{
//       //从pinia取token
//       const userStore = useUserStore()
//       const {token} = userStore
//       // console.log(token)
//       // console.log(userStore.$state.token)
//       if(token){
//           //token为true 说明用户已经登录
//           if(to.path === '/login'){
//               return '/'
//           }else{
//               //放行
//               return true
//           }
//       }else{
//           //未登录
//           if(witeList.includes(to.path)){
//               return true
//           }else{
//               //未登录，跳转到登录界面，
//               //并且如果登录后需要跳转到某个界面，就需要携带回跳地址
//               return `/login?redirectUrl=${to.path}`//反引号可以通过${}嵌入表达式的值
//           }
//       }
// })
export default router